<?php

namespace common\modules\admin\models\search;

use yii\base\Model;
use common\components\ActiveDataProvider;
use common\modules\admin\models\Admin;

/**
 * AdminSearch represents the model behind the search form about `oss\models\Admin`.
 */
class AdminSearch extends Admin
{
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['admin_id', 'deleted', '_intm', '_uptm', 'position'], 'integer'],
            [['username', 'password', 'salt', 'nick', 'idcard', 'phone','name'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = Admin::find();

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }
        if(strlen($this->deleted) == 0){
            $this->deleted = Admin::DELETED_NO;
        }
        $query->andFilterWhere([
            'admin_id' => $this->admin_id,
            'deleted' => $this->deleted,
            'position' => $this->position,
            '_intm' => $this->_intm,
            '_uptm' => $this->_uptm,
        ]);

        $query->andFilterWhere(['like', 'username', $this->username])
            ->andFilterWhere(['like', 'name', $this->name])
            ->andFilterWhere(['like', 'password', $this->password])
            ->andFilterWhere(['like', 'nick', $this->nick])
            ->andFilterWhere(['like', 'phone', $this->phone])
            ->andFilterWhere(['like', 'idcard', $this->idcard]);

        return $dataProvider;
    }
}
